package com.xiyili.topspecialty;

import java.io.File;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ShareActivity extends BaseFragmentActivity {

	private String _file_absolute_path;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share);
		// Show the Up button in the action bar.
		setupActionBar();
		initViews();
	}

	private void initViews() {
		TextView tipView = (TextView) findViewById(R.id.tip);
		Bundle extras = getIntent().getExtras();
		String file_dir = extras.getString("FILE_DIR");
		String file_name = extras.getString("FILE_NAME");
		_file_absolute_path = extras.getString("FILE_ABSOLUTE_PATH");
		StringBuilder sb = new StringBuilder();
		sb.append("已经将安装包(");
		sb.append(file_name);
		sb.append(")导出到");
		sb.append(file_dir);
		sb.append("文件夹\n你可以将安装包分享给朋友啦");
		tipView.setText(sb.toString());

		Button shareButton = (Button)findViewById(R.id.btn_share);
		shareButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				share();
			}
		});


	}

	protected void share() {
		Intent shareIntent = new Intent();
		String type = "application/vnd.android.package-archive";
		Uri uriToApk = Uri.fromFile(new File(_file_absolute_path));
		shareIntent.setAction(Intent.ACTION_SEND);
		shareIntent.putExtra(Intent.EXTRA_STREAM, uriToApk);
		shareIntent.setType(type);
		startActivity(Intent.createChooser(shareIntent, "分享 via"));

	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.share, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
